package cn.bdqn.controller;

import cn.bdqn.bean.result.Result;
import cn.bdqn.bean.result.ResultCode;
import cn.bdqn.bean.result.ResultGenerator;
import cn.bdqn.domain.Recipe;
import cn.bdqn.service.RecipeService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 处方controller
 */
@RestController
@RequestMapping("/PM/recipe")
public class RecipeController {

    @Autowired
    private RecipeService recipeService;

    /**
     * 查询所有的处方
     *
     * @param map 返回的数据集合的封装
     * @return 封装好的结果
     * @throws Exception
     */
    @RequestMapping("/getAll")
    public Result getAllRecipe(Map<String, Object> map) throws Exception {

        try {
            List<Recipe> recipes = recipeService.queryAll();
            map.put("recipeList", recipes);
            return ResultGenerator.genSuccessResult(map);

        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genFailResult("查询有误", ResultCode.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 根据id修改配药信息
     *
     * @param id
     * @return 修改信息
     * @throws Exception
     */
    @RequestMapping("/editById/{id}")
    public Result editRecipeIfallocation(@PathVariable("id") Integer id) throws Exception {

        try {
            int res = recipeService.editRecipeById(id);
            if (res == 1) {

                return ResultGenerator.genSuccessResult("修改成功");
            } else {
                return ResultGenerator.genFailResult("数据有误", ResultCode.INTERNAL_SERVER_ERROR);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genFailResult("出现异常", ResultCode.INTERNAL_SERVER_ERROR);
        }
    }


}
